<template>
        <!-- 导航条 -->
    <div class="searchTop">
        <!-- 返回 -->
        <div class="searchTopNav">
            <div class="back" @click="back">
                <svg class="icon" aria-hidden="true">
                    <use xlink:href="#icon-sdf"></use>
                </svg>
            </div>
            <!-- 搜索 -->
            <div class="right">
               <input type="text" v-model="searchKeyword" :placeholder="placeholder" @keydown.enter="saveKeyWord">
            </div>
        </div>
        <!-- 历史搜索 -->
        <div class="history" v-show="searchSongs.length == 0">
            <div class="historyLeft">
                历史
            </div>
            <div class="historyRight">
                <div @click="historySearch(item)" class="keywordItem" v-for="(item,index) in keywordList" :key="index">{{item}}</div>
            </div>
        </div>



    <div class="searchSongs">
        <!-- 播放全部 -->
        <div class="searchSongs-top" v-show="searchSongs.length !== 0">
            <div class="left">
                <svg class="icon" aria-hidden="true">
                    <use xlink:href="#icon-bofang3"></use>
                </svg>
                <div class="text">
                    <div class="title">播放全部</div>
                    <div class="num">(共{{searchSongs.length}}首)</div>
                </div>
            </div>
        </div>
        <!-- 播放列表 -->
        <div class="list">
            <div class="playItem" v-for="(item, index) in searchSongs" :key="index">
                <div class="left">
                    <div class="index">{{index+1}}</div>
                    <div class="content">
                        <div class="title">{{item.name}}</div>
                        <div class="anthor">
                            <span class="name">{{item.album.name}}</span>
                        </div>
                    </div>
                </div>

                <div class="right">
                    <svg class="icon" aria-hidden="true" @click="setPlay(item, index)">
                        <use xlink:href="#icon-bofang2"></use>
                    </svg>
                    <svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-liebiao"></use>
                    </svg>
                </div>
            </div>
        </div>
    </div>




    </div>
</template>

<script>
import {searchMusic} from '@/api/index.js'
export default {
    name: "searchTop",
    components: {
        searchMusic
    },
    data:function(){
        return {
            placeholder:"陈奕迅",
            // 保存起来
            keywordList:[],
            searchKeyword:'',
            // 搜索到的歌曲保存到searchSongs:[]
            searchSongs:[]
        }
    },
    beforeMount() {
            //设置历史模式                                从localStorage 取出 keywordList  
        this.keywordList =localStorage.keywordList? JSON.parse(localStorage.keywordList):[]
        // this.keywordList = JSON.parse(localStorage.keywordList)
    },
    methods: {
        //保存函数
        saveKeyWord: async function() {
            //每次回车都要push一次
            this.keywordList.push(this.searchKeyword)
            this.keywordList = Array.from(new Set(this.keywordList))
            if(this.keywordList.length > 10) {
                this.keywordList = this.keywordList.slice(this.keywordList.length-10,this.keywordList.length)
            }
            //   this.keywordList 当前列表  通过JSON.stringify 变成字符串  保存 localStorage.keywordList 里
            localStorage.keywordList = JSON.stringify(this.keywordList)
            //获取数据
            let result = await searchMusic(this.searchKeyword)
            this.searchSongs = result.data.result.songs
            console.log(this.searchSongs);
        },
        back: function() {
            this.$router.go(-1);
        },
        historySearch: function(keword) {
            this.searchKeyword = keword;
            this.saveKeyWord()
        },
        setPlay(item, i) {
            item.al = item.album;
            
            this.$store.commit('pushPlaylist', item)
            this.$store.commit('setPlayIndex',this.$store.state.playlist.length-1)
        }
    }
}
</script>

<style lang="less" scoped>
.searchTop {
    width: 7.5rem;
    height: calc(100vh- 1.2rem);
    padding: 0 0.4rem;
    .searchTopNav{
        display: flex;
        width: 100%;
        height: 1.2rem;
        padding:0 0.4rem;
        align-items: center;
        // position: fixed;
        // top: 0;
        // left: 0;
        .icon{
            width: 0.45rem;
            height: 0.45rem;
        }
        .right {
            padding: 0 0 0 0.4rem;
            flex: 1;
            input {
                border: none;
                outline: none;
                border-bottom: 1px solid #666;
                width: 100%;
                height: 0.5rem;
            }
        }
    }
    .history {
        display: flex;
        .historyLeft {
            width: 1.2rem;
            height: 0.6rem;
            font-weight: 900;
            margin:0.2rem 0rem;
            line-height: 0.6rem;
        }
        .historyRight {
            color: #666;
            display: flex;
            flex-wrap: wrap;
            flex: 1;
            .keywordItem {
                background-color: #eee;
                height: 0.6rem;
                padding: 0 0.2rem;
                border-radius: 0.4rem;
                line-height: 0.6rem;
                margin:0.2rem 0.1rem;
            }
        }
    }
    .searchSongs {
        width: 7.5rem;
        padding: 0 0.4rem;
        background-color: #fff;
        border-top-left-radius: 0.3rem;
        border-top-right-radius: 0.3rem;
        margin-top: 0.4rem;
    .searchSongs-top {
        display: flex;
        justify-content: space-between;
        height: 1rem;
        align-items: center;
        .left {
            display: flex;
            align-items: center;
            color: #666;
            .icon {
                width: 0.6rem;
                height: 0.6rem;
            }
            .title {
                font-size: 0.34rem;
                font-weight: 900;
            }
            .num {
                font-size: 0.24rem;
                color: #666;
            }
            .text {
                display: flex;
                align-items: center;
                margin-left: 0.2rem;
            }
        }
        .btn {
            font-size: 0.24rem;
            color: #fff;
            background-color: orangered;
            line-height: 0.6rem;
            padding: 0.1rem;
            border-radius: 0.4rem;
        }
    }
    .list {
        .playItem {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 1rem;
            .left {
                display: flex;
                align-items: center;
                .index {
                    width: 0.4rem;
                }
                .content {
                    margin-left: 0.4rem;
                }
                .title {
                    font-size: 0.3rem;
                    font-weight: 900;
                    color: #000;
                    margin-bottom: 0.1rem;
                }
                .tag {
                    font-size: 0.2rem;
                    color: orangered;
                    border: 1px solid orangered;
                    border-radius: 0.1rem;
                    margin-right: 0.08rem;
                }
                .author {
                    color: #666;
                }
            }
            .right {
                .icon {
                    margin: 0 0.2rem;
                }
            }
        }
    }
}
}
</style>